package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.feign.cart.CartFeignClient;
import com.atguigu.gmall.model.product.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 钟涛
 * @version 1.0
 * @description: TODO
 * @date 2021/12/22 11:11
 */
@Controller
public class CartController {
    @Autowired
    private CartFeignClient cartFeignClient;
//    public static ThreadLocal<HttpServletRequest> requestThreadLocal = new ThreadLocal<>();
//    private static ThreadLocal threadLocal = new ThreadLocal();
    @GetMapping("/cart.html")
    public String cartListPage() {
        return "cart/index";
    }

    @GetMapping("/addCart.html")
    public String addSkuToCart(@RequestParam("skuId") long skuId,
                               @RequestParam("skuNum") Integer skuNum,
                               Model model, HttpServletRequest request) {
//        requestThreadLocal.set(request);
        //feign 有拦截器机制，利用feign的拦截器机制把老请求的头都哪来放在新请求里面即可
        SkuInfo skuInfo = cartFeignClient.addSkuToCart(skuId, skuNum);
        //页面需要的数据  skuInfo、skuNum
        model.addAttribute("skuInfo",skuInfo);
        model.addAttribute("skuNum",skuNum);
        return "cart/addCart";
    }
}
